package com.hht.webpackagekit.core;

import android.net.Uri;
import android.text.TextUtils;

import com.hht.webpackagekit.core.util.Logger;

import java.util.List;

/**
 * 资源请求键
 */
public class ResourceKey {
    private final String host;
    private final String schema;
    private final List<String> pathList;
    private final String originUrl;
    private final String path;

    public ResourceKey(String url) {
        Uri uri = Uri.parse(url);
        originUrl = url;
        host = uri.getHost();
        schema = uri.getScheme();
        pathList = uri.getPathSegments();
        if(url.contains("file:///android_asset")){
            path = url.substring("file:///android_asset/".length());
        } else {
            int port = uri.getPort();
            if(port != -1){
                String portStr = String.valueOf(port);
                int index = url.indexOf(portStr) + portStr.length() + 1;
                path = url.substring(index);
                Logger.d("port != -1, url = " + url + " index = " + index + " path = " + path);
            } else {
                if(!TextUtils.isEmpty(host)){
                    int index = url.indexOf(host) + host.length() + 1;
                    path = url.substring(index);
                    Logger.d("host not empty, host = " + host + " index = " + index + " path = " + path);
                } else{
                    path = url;
                    Logger.e("ResourceKey constructor host empty, path = " + path);
                }
            }
        }
        Logger.d( "ResourceKey constructor, url = " + url + " host = " + host
                + " schema = " + schema + " path = " + path + " pathList = " + pathList);
    }

    @Override
    public int hashCode() {
        int result = 17;
//        result = result * 37 + hashNotNull(host);
//        result = result * 37 + hashNotNull(schema);
//        if (pathList != null) {
//            for (String pathSeg : pathList) {
//                result = result * 37 + hashNotNull(pathSeg);
//            }
//        }
        result = result * 37 + hashNotNull(path);
        return result;
    }

    private int hashNotNull(Object o) {
        return o == null ? 0 : o.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof ResourceKey)) {
            return false;
        }

        ResourceKey that = (ResourceKey) obj;
        if (!TextUtils.equals(path, that.path)) {
            return false;
        }
        return true;

//        if (!TextUtils.equals(host, that.host)) {
//            return false;
//        }
//        if (!TextUtils.equals(schema, that.schema)) {
//            return false;
//        }
//        if (this.pathList == that.pathList) {
//            return true;
//        }
//        if (pathList == null && that.pathList != null) {
//            return false;
//        }
//        if (pathList != null && that.pathList == null) {
//            return false;
//        }
//        boolean isEqual = true;
//        for (String pa : pathList) {
//            if (!that.pathList.contains(pa)) {
//                isEqual = false;
//                break;
//            }
//        }

//        return isEqual;
    }

    @Override
    public String toString() {
        return "ResourceKey{" +
                "host='" + host + '\'' +
                ", schema='" + schema + '\'' +
                ", pathList=" + pathList +
                ", originUrl = " + originUrl +
                '}';
    }
}
